//
//  RouString.swift
//  RouSwift
//
//  Created by wangkun on 2017/5/5.
//  Copyright © 2017年 leecong. All rights reserved.
//

import Foundation
import UIKit

public class RouTools {
    
    private static let keychain = RouTools.getKeychain()
    static let bundle = Bundle(for: RouTools.self)
    
    public static func bundlePath(_ name: String, _ type: String, _ directory: String) -> String {
        return RouTools.bundle.path(forResource: name, ofType: type, inDirectory: directory) ?? ""
    }
    
    public static var UDID : String! {
        get {
            var udid = keychain[string: "deviceuuid"]
            if udid == nil || udid?.count == 0 {
                udid = UIDevice.current.identifierForVendor?.uuidString
                udid = (udid ?? "") + String(Date().timeIntervalSince1970)
                udid = udid?.md5()
                keychain["deviceuuid"] = udid
            }
            return udid ?? ""
        }
    }
    
    public static func changeUdid(_ udid: String) -> String! {
        if UDID == udid {
            keychain[string: "deviceuuid"] = ""
        }
        return RouTools.UDID
    }
    
    
    public static var APPKEY : String! {
        get {
            var udid = RouTools.UDID
            return udid?.shangyouSecurity()
        }
    }
    
    public static func saveKeychainInfo(info : Any, forKey: String) {
        let data : Data = NSKeyedArchiver.archivedData(withRootObject: info) as Data
        do {
            try keychain.set(data, key: forKey)
        } catch {
            
        }
    }
    
    public static func getKeychainInfo(forKey: String) -> Any {
        var data = Data()
        do {
            data = try keychain.getData(forKey) ?? Data()
        } catch { }
        return NSKeyedUnarchiver.unarchiveObject(with: data) ?? 0
    }
    
    public static func deleteKeychainInfo(forKey: String) {
        do {
            try keychain.remove(forKey)
        } catch{ }
    }
    
    ///MAKE: - 私有方法
    private static func getKeychain() -> Keychain {
        let dic = Bundle.main.infoDictionary
        let bundle = dic?["CFBundleIdentifier"]
        
        return Keychain(service: bundle as! String)
    }
    
}

public struct Device {
    
    static let Width = UIScreen.main.bounds.size.width
    static let Height = UIScreen.main.bounds.size.height
    
    public enum DeviceType {
        case iphone4
        case iphone5
        case iphone6
        case iphone6p
    }
    
    //判断屏幕类型
    public static func size() -> DeviceType {
        
        switch (Width, Height) {
        case (320, 480),(480, 320):
            return .iphone4
        case (320, 568),(568, 320):
            return .iphone5
        case (375, 667),(667, 375):
            return .iphone6
        case (414, 736),(736, 414):
            return .iphone6p
        default:
            return .iphone6
        }
    }
    
    public static let isSimulator: Bool = {
        var isSim = false
        #if arch(i386) || arch(x86_64)
            isSim = true
        #endif
        return isSim
    }()
}


